// This is the main DLL file.
#include "stdafx.h"
#include "cvLib.h"

void cvLib::OurMethod::createWindows()
{
	namedWindow( "Tracking" , CV_WINDOW_AUTOSIZE );
}
void cvLib::OurMethod::initTracker()
{	
	cvSetMouseCallback(winName,on_mouse,this->msTrackPtr);

	int fWidth = 640;//cvGetCaptureProperty(msTrackPtr->capture,CV_CAP_PROP_FRAME_WIDTH);
	int fHeight = 480;//cvGetCaptureProperty(msTrackPtr->capture,CV_CAP_PROP_FRAME_HEIGHT);

	trackObjPtr->setupImages(fWidth,fHeight);
	for (int i = 0;i < this->objectNum;i++)
	{
		trackSet[i]->setupImages(fWidth,fHeight);
	}

	// Calibration
	FILE     *data_fd;
	if ((data_fd = fopen ("G:\\Code\\tracking\\PTZCameraCapture\\cvLib\\staticCam_cp.dat", "r")) == NULL) {
		(void) fprintf (stderr, "%s: unable to open file \"%s\"\n", "haha", "hehe");
		exit (-1);
	}
	load_cp_cc_data (data_fd, caliParaPtr, caliConstPtr);
	fclose (data_fd);

}
void cvLib::OurMethod::methodRunLocal()
{
	int w = 640,h = 480;	
	//cv::Mat imgMat = img;

	//int k = nSize - 1;//921677;

	//for (int i = 0; i < h; i++)
	//{
	//	for (int j = 0; j < w; j++)
	//	{
	//		imgMat.at<Vec3b>(i, w-1-j)[0] = pData[k--];
	//		imgMat.at<Vec3b>(i, w-1-j)[2] = pData[k--];
	//		imgMat.at<Vec3b>(i, w-1-j)[1] = pData[k--];
	//	}
	//}
	//if (!msTrackPtr->tractObject)
	//{
	//	imshow( "Tracking", imgMat);
	//}
	//else
	//{
	//	cv::Mat tmp = msTrackPtr->frame;
	//	imshow( "Tracking", tmp);
	//}

	CvCapture* capture = cvCaptureFromFile("C:\\Users\\a0068962\\Desktop\\data\\AMC_3_0_1.asf");

	while(true)
	{
		// Tracking
		//msTrackPtr->frame 
		img	= cvQueryFrame(capture);//img;
		msTrackPtr->frame = img;

		//if (!msTrackPtr->tractObject)
		//{
		//	imshow( "Tracking", imgMat);
		//}
		//else
		//{

		//}

		if(!(msTrackPtr->frame))
		{
			//trackObj.copyImage(msTrack.frame);
			return;
		}

		if(msTrackPtr->onProcess && msTrackPtr->tmpSelection.height > 0 && msTrackPtr->tmpSelection.width > 0)
		{
			msTrackPtr->drawRectangles();
			//msTrack.rectCount ++;
			//std::cout << "number of objects selected.." << msTrack.rectCount << std::endl;
		}

		trackObjPtr->copyImage(msTrackPtr->frame);

		if(msTrackPtr->tractObject)
		{		
			for(int j = 0; j< msTrackPtr->rectCount ; j++){
				(*(trackSet[j])).copyImage(msTrackPtr->frame);
				(trackSet[j])->setTrackWindow(msTrackPtr->tmpSelection);
				(trackSet[j])->convertToHSV();
				(trackSet[j])->startTrackObject((*caliParaPtr),(*caliConstPtr));
				*(objectLocations[j]) = (trackSet[j])->getTrackWindow();
				trackObjPtr->direction = (trackSet[j])->direction;
				trackObjPtr->x = (trackSet[j])->x;
				trackObjPtr->y = (trackSet[j])->y;
				trackObjPtr->y = (trackSet[j])->y;
			}
			for(int j = 0; j< msTrackPtr->rectCount ; j++){

				trackObjPtr->drawRectangle(*(objectLocations[j]));
				trackObjPtr->drawDirection();
			}
			//finishedLocating = true;
			//trackObj.drawRectangle(myRect);
			//msTrack.frame = trackSet[0].getTrackedFrame();

			msTrackPtr->frame = cvCloneImage(trackObjPtr->getTrackedFrame());

			//msTrack.tractObject = false;

		}

		cv::Mat tmp = msTrackPtr->frame;
		imshow( "Tracking", tmp);


		if(msTrackPtr->tractObject)
		{
			szCurPeopleOM = msTrackPtr->rectCount;

			for(int i=0;i<msTrackPtr->rectCount;i++)
			{
				dataLocXPtr->at(i) = (trackSet[i])->x;
				dataLocYPtr->at(i) = (trackSet[i])->y;
				/*image_coord_to_world_coord ((trackSet[i])->x, (trackSet[i])->y, 0, dataLocXPtr->at(i), dataLocYPtr->at(i),(*caliParaPtr),(*caliConstPtr));*/
				dataLocOriPtr->at(i) = (trackSet[i])->direction;
			}

			hm->szCurPeopleHM = msTrackPtr->rectCount;
			//hm->dataSzPeople = msTrackPtr->rectCount;
			//hm->heatMapCalc(*(dataLocXPtr), *(dataLocYPtr), *(dataLocOriPtr));

		}
		cvWaitKey(50);
	}

	// ToDo :
}
void cvLib::OurMethod::methodRun(array<unsigned char>^ pData, int nSize)
{
	const int w = 640,h = 480;	
	cv::Mat imgMat = img;

	int k = nSize - 1;//921677;

	for (int i = 0; i < h; i++)
	{
		for (int j = 0; j < w; j++)
		{
			imgMat.at<Vec3b>(i, w-1-j)[0] = pData[k--];
			imgMat.at<Vec3b>(i, w-1-j)[2] = pData[k--];
			imgMat.at<Vec3b>(i, w-1-j)[1] = pData[k--];
		}
	}
	if (!msTrackPtr->tractObject)
	{
		imshow( "Tracking", imgMat);
	}
	else
	{
		cv::Mat tmp = msTrackPtr->frame;
		imshow( "Tracking", tmp);
	}

	// Tracking
	msTrackPtr->frame = img;

	if(!(msTrackPtr->frame))
	{
		//trackObj.copyImage(msTrack.frame);
		return;
	}

	if(msTrackPtr->onProcess && msTrackPtr->tmpSelection.height > 0 && msTrackPtr->tmpSelection.width > 0)
	{
		msTrackPtr->drawRectangles();
		//msTrack.rectCount ++;
		//std::cout << "number of objects selected.." << msTrack.rectCount << std::endl;
	}

	trackObjPtr->copyImage(msTrackPtr->frame);

	if(msTrackPtr->tractObject)
	{		
		for(int j = 0; j< msTrackPtr->rectCount ; j++){
			(*(trackSet[j])).copyImage(msTrackPtr->frame);
			(trackSet[j])->setTrackWindow(msTrackPtr->tmpSelection);
			(trackSet[j])->convertToHSV();
			(trackSet[j])->startTrackObject((*caliParaPtr),(*caliConstPtr));
			*(objectLocations[j]) = (trackSet[j])->getTrackWindow();
			trackObjPtr->direction = (trackSet[j])->direction;
			trackObjPtr->x = (trackSet[j])->x;
			trackObjPtr->y = (trackSet[j])->y;
			trackObjPtr->y = (trackSet[j])->y;
		}
		for(int j = 0; j< msTrackPtr->rectCount ; j++){

			trackObjPtr->drawRectangle(*(objectLocations[j]));
			trackObjPtr->drawDirection();
		}
		//finishedLocating = true;
		//trackObj.drawRectangle(myRect);
		//msTrack.frame = trackSet[0].getTrackedFrame();

		msTrackPtr->frame = cvCloneImage(trackObjPtr->getTrackedFrame());

		//msTrack.tractObject = false;

	}

	if(msTrackPtr->tractObject)
	{
		szCurPeopleOM = msTrackPtr->rectCount;

		for(int i=0;i<msTrackPtr->rectCount;i++)
		{
			dataLocXPtr->at(i) = (trackSet[i])->x;
			dataLocYPtr->at(i) = (trackSet[i])->y;
			/*image_coord_to_world_coord ((trackSet[i])->x, (trackSet[i])->y, 0, dataLocXPtr->at(i), dataLocYPtr->at(i),(*caliParaPtr),(*caliConstPtr));*/
			dataLocOriPtr->at(i) = (trackSet[i])->direction;
		}

		hm->szCurPeopleHM = msTrackPtr->rectCount;
		//hm->dataSzPeople = msTrackPtr->rectCount;
		hm->heatMapCalc(*(dataLocXPtr), *(dataLocYPtr), *(dataLocOriPtr),hm->para);

	}

	// ToDo :


}
bool cvLib::OurMethod::getImagefromCS(array<unsigned char>^ pData, int nSize)
{	
	int w = 640,h = 480;
	cv::Mat img(h,w, CV_8UC3, Scalar(0,0,255));

	int k = nSize - 1;//921677;

	for (int i = 0; i < h; i++)
	{
		for (int j = 0; j < w; j++)
		{
			img.at<Vec3b>(i, w-1-j)[0] = pData[k--];
			img.at<Vec3b>(i, w-1-j)[2] = pData[k--];
			img.at<Vec3b>(i, w-1-j)[1] = pData[k--];
		}
	}
	imshow( "Tracking", img );
	return true;
}
